//比较时间大小 只比较时分秒
export function compareTime (date1, date2) {
  var time1 = [date1.getHours(), date1.getMinutes(), date1.getSeconds()];
  var time2 = [date2.getHours(), date2.getMinutes(), date2.getSeconds()];

  for (var i = 0; i < 3; i++) {
    if (time1[i] > time2[i]) return -1;
    if (time1[i] < time2[i]) return 1;
  }

  // 如果所有时间都相等，则返回0
  return 0;
}

//对象数组去重
/** 数组去重
 * 思路：定义一个新数组，存放原数组的第一个元素，然后将原数组和新数组的元素对比，若不同则存放在新数组中
 * @param arr 需要去重的数组
 * @param t 根据 t 字段(指定)进行去重
 * @returns {*[]} 已去重后的数据
 */
export function deduplicate(arr, t = "") {
  const newArr = [arr[0]]
  // 有指定字段
  if (t) {
    for (let i = 1; i < arr.length; i++) {
      let repeat = false
      for (let j = 0; j < newArr.length; j++) {
        if (t && arr[i][t] === newArr[j][t]) {
          repeat = true
          break
        }
      }
      if (!repeat) newArr.push(arr[i])
    }
  }
  // 无指定字段
  else {
    for (let i = 1; i < arr.length; i++) {
      let repeat = false
      for (let j = 0; j < newArr.length; j++) {
        if (arr[i] === newArr[j]) {
          repeat = true
          break
        }
      }
      if (!repeat) newArr.push(arr[i])
    }
  }
  return newArr
}


// json转xlsx
import { utils, writeFile } from 'xlsx';
export function change(lat) {
  const jsonData = lat
  const worksheet = utils.json_to_sheet(jsonData);
  const workbook = utils.book_new();
  utils.book_append_sheet(workbook, worksheet, 'Sheet1');

  const excelData = writeFile(workbook, 'data.xlsx', { type: 'blob', bookType: 'xlsx' });

  const url = URL.createObjectURL(excelData);
  const link = document.createElement('a');
  link.href = url;
  link.download = 'data.xlsx';
  link.click();
  URL.revokeObjectURL(url);
}


// 整理对象数组
export function goodsidsRecombineToNewJson(old_json) {
	var goodsids = {};
	for (var code in old_json) {
		var goodsid = old_json[code].department;
		if (!goodsids[goodsid]) {
			goodsids[goodsid] = [];
		}
		goodsids[goodsid].push(old_json[code]);
	}
	return goodsids;
}

// 判断车辆是否迟到早退
export function carLateAndEarlyDeparture(InTime,outTime) {
  let late = ''
  let early = ''
  let a = ''
  if(InTime && compareTime(new Date('2024.04.01 08:35:00'), new Date(InTime)) == 1) {
    late = '迟到'
  } 
  if(outTime && compareTime(new Date('2024.04.01 17:00:00'), new Date(InTime)) == -1) {
    early = '早退'
  }
  return a = late + '' + early
}

